package finn.movingheadtracker;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class UniverseArtnet extends Universe{
	public int ArtnetUniverseId;
	public int SubnetUniverseId;
	byte[] values = new byte[512];
	
	public UniverseArtnet(int id, String name, short direction, short type) {
		super.id = id;
		super.name = name;
		super.direction = direction;
		super.type = type;
	}

	@Override
	public void change(DmxValue[] values) {
		if(this.direction == Universe.OUT){
			for(DmxValue value: values){
				this.values[value.addr] = value.value;
			}
		}
		if(this.direction == Universe.IN){
			this.refresh(values);
			DmxMerger.setUniverseDmx(id, values);
		}
	}
	
	@Override
	public void change(byte[] values){
		List<DmxValue> new_values = new ArrayList<DmxValue>();
		for(int i = 0; i < values.length; i++){
			if(values[i] != this.values[i]){
				new_values.add(new DmxValue(i, values[i]));
			}
		}
		if(!new_values.isEmpty()){
			this.change((DmxValue[]) new_values.toArray());
		}
	}
	/**
	 * Refreshes the byte[] values
	 * @param values The new DmxValues
	 * @return Highest changed Dmx-Channel
	 */
	public int refresh(DmxValue[] values){
		for(DmxValue value : values){
			this.values[value.addr] = value.value;
		}
		List<DmxValue> convertedList = Arrays.asList(values);
		return Collections.max(convertedList).addr;
	}

	@Override
	public byte getChannelValue(int addr) {
		return values[addr];
	}

	
}
